<?php
 include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."pagesettings/master.inc");
include(DOCINCLUDES."global_files/global_js_form.inc");
include(DOCINCLUDES."global_files/global_functions.inc");
include(DOCINCLUDES."global_files/global_report.inc");
include(DOCINCLUDES."global_files/global_errorpage.inc");
$relation=($_REQUEST["rel_type"])?$_REQUEST["rel_type"]:"buy";
$action=($_REQUEST["action"])?$_REQUEST["action"]:"ent";
$rel_type=array("buy"=>"Buyer","act"=>"Account");

// $form = new cForm();
// $db     = new cDatabase($company_id);
// $sql    = new pgSql();
$report = new cReport();
$report->sql=$sql;
$report->db=$db;
// $db->setSearchPath();
// $dv=new cDataValidation();

if($_POST)
	{
	$dv->validations["code"]=array("isEmpty"=>"Enter Code");
	$dv->validations["company_id"]=array("isEmpty"=>"Select Company");
	$dv->validate($_POST);
		if(!$dv->errors)
			{
			$db->beginTransaction();
			if($relation=="buy")
				{
				$commercial_details["buyer_code"]=$_POST["code"];
				$commercial_details["buyer_name"]=$_POST["company_id"];
				if($_POST["remarks"])
				$commercial_details["credit_days"]=$_POST["remarks"];
				if($_POST["currency_id"])
				$commercial_details["currency_name"]=$_POST["currency_id"];
				if($_POST["prim_port_id"])
				$commercial_details["port_id"]=$_POST["prim_port_id"];
				$sql->table ="_jbuyer";
				$sql->column =$commercial_details;
				$sql->generateInsertQuery();
				$db->query = $sql->query;
				$db->runQuery();
				}
				else if($relation=="act")
				{
				$commercial_details["account_code"]=$_POST["code"];
				$commercial_details["account_name"]=$_POST["company_id"];
				if($_POST["remarks"])
				$commercial_details["credit_days"]=$_POST["remarks"];
				$sql->table ="_jaccount";
				$sql->column =$commercial_details;
				$sql->generateInsertQuery();
				$db->query = $sql->query;
				$db->runQuery();
				}
			
			if(!$db->errors)
				{
				$db->endTransaction();
				unset($_POST);
				header("Location:master_relationship.php?rel_type=$relation");
				exit;
				} 
			}
	}
else
	{
	switch ($action)
		{
		case "add":
			$table_name="_j".strtolower(str_replace (" ","",$rel_type[$relation]));
			$col_name=strtolower(str_replace (" ","",$rel_type[$relation]));
			$db->query="SELECT company_id FROM _mcompany con JOIN $table_name rt ON rt.".$col_name."_name=con.company_name WHERE rt.".$col_name."_jid = ".$_REQUEST["id"];
			$db->runQuery();
			$company_id_array=$db->returnArrays();
			header("location:master_contact.php?company_id=".$company_id_array[0]["company_id"]);
			break;
		case "edit":
			//
			break;
		case "del":
			//
			break;
		}
	}
					
$page->title=$page->heading="$rel_type[$relation] Details";
$page->doHeaderHtml();
	echo "<br/><div align=\"center\">";
	echo "<a href=\"master_company_contact.php\">Add Contact</a>";

	$form->properties = array("name"=>"master_relationship","method"=>"POST","action"=>$_SERVER["PHP_SELF"]);
	echo $form->startForm();

        $form->properties=array("type"=>"hidden","name"=>"relation","value"=>$relation);
        echo $form->generateInputTag();
	$form->properties = array("name"=>"code","type"=>"text");
	$item_list[0][$rel_type[$relation]." Code"] = $form->generateInputTag();
	$arrays=getMaster("company");
	$form->properties = array("name"=>"company_id");
	$form->select_tag_properties=array("arrays"=>$arrays);
	$item_list[0][$rel_type[$relation]." Name"] = $form->generateSelectTag();
	if($relation=="buy")
		{
		$arrays=getMaster("currency");
		$form->properties = array("name"=>"currency_id");
		$form->select_tag_properties=array("arrays"=>$arrays);
		$item_list[0]["Currency"] = $form->generateSelectTag();
		$arrays=getMaster("city");
		$form->properties = array("name"=>"prim_port_id");
		$form->select_tag_properties=array("arrays"=>$arrays);
		$item_list[0]["Destination"] = $form->generateSelectTag();
		}

	$form->properties = array("name"=>"remarks","type"=>"text","size"=>"15");
	$item_list[0]["Remarks"] = $form->generateInputTag();
	
	$report->customization["table_style"]="width:30%; border-style:solid; border-width:thin; border-collapse:collapse;";
	$report->customization["tr_style"]="border-style:none; border-width:thin; border-collapse:collapse";
	$report->title="$rel_type[$relation] Entry";
	$report->highlight=true;
	$report->data=$item_list;
	$report->doVerticalTable();
	$form->properties = array("name"=>"save","type"=>"button","Value"=>"Save","onclick"=>"testconform()");
	echo $form->generateInputTag();
	echo $form->endForm();
	

if($relation=="act")
	{
	$db->query="SELECT mc.company_id, ja.account_code as \"Code\",mc.company_name as \"Account\",ja.credit_days as \"Remarks\" from _jaccount ja join _mcompany mc on mc.company_id=ja.account_name order by account_code;";
	$db->runQuery();
	$view_list=$db->returnArrays();
	}
else
	{
	$db->query="SELECT mc.company_id, jb.buyer_code as \"Code\",mc.company_name as \"Buyer\",mcy.currency_name as \"Currency\",mct.city_name as \"City\",jb.credit_days as \"Remarks\" from _jbuyer jb join _mcompany mc on mc.company_id=jb.buyer_name left join _mcity mct on mct.city_id=jb.port_id left join _mcurrency mcy on mcy.currency_id=jb.currency_name order by buyer_code;";
	$db->runQuery();
	$view_list=$db->returnArrays();
	}

$cnt=count($view_list);
for($i=0;$i<$cnt;$i++)
	{
	if($_GET["rtype"] != "p")
		{
		$company_id=$view_list[$i]["company_id"];
		$report->query_string=array("company_id"=>$company_id);
		$view_list[$i]["Contact"]=$report->returnLink("./master_company_contact.php","View");
		unset($view_list[$i]["company_id"]);
		}
	}
	$report->customization["table_style"]="width:50%; border-style:solid; border-width:thin; border-collapse:collapse;";
	$report->customization["tr_style"]="border-style:none; border-width:thin; border-collapse:collapse";
	$report->title="$rel_type[$relation] Commercial Details";
	$report->data=$view_list;
	echo "</br>";
	echo "</br>";
	$report->setNA();
	$report->doHorizontalTable();
	echo "</div><br />";

$page->doFooterHtml();

echo "<script type=\"text/Javascript\">";
echo "var rel = \"$relation\";";
echo "</script>";

?>
<script type="text/Javascript">
function master_pop(master_id,element,form)
	{
	var new_page = "master_pop.php?master_id="+master_id+"&element="+element+"&form="+form;
	window.open(new_page,"","width=250,height=160,scrollbars=no,alwaysRaised=yes,status=no,top=200,left=500");
	}
function testconform()
{	
	var ok = confirm("Do you want to save");
	if(ok)
	{
	var code =  document.master_relationship.code.value;
	var name =  document.master_relationship.company_id.value;
	if ((code != '') && (name !=''))
		{
		document.master_relationship.action="master_relationship.php?rel_type="+rel;
		document.master_relationship.submit();
		}
		else
		{
		alert("Enter Value");
		}
	}
}	
</script>
